/*
 * AnimMenu.java
 * Created by: Crecen Carr (JakeSamiRulz)
 * Co-created by: Clinton Nolan (Uru)
 *
 * A animation menu system for Overlay Animations.
 */

package AnimationSystem;

import ScreenSystem.Screen;
import ScreenSystem.ScreenStack;
import ImageSystem.ImageRegistry;
import LuaSystem.LuaEngine;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.Thread;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JComponent;

public class AnimMenu extends Screen implements Runnable{
    private String contents;//contents of a file
    LinkedList<AnimationFrame> animationList;//List of animations
    private int controlSpeed;//the speed (millisec) in Thread
    private boolean regulate;//regulates the image rate
    Thread control;//the thread that controls the rate
    private Vector<LuaAction> sortAction;//sorts all key actions
    private int currentgroup;//the current group of actions
    
    public AnimMenu(String filename){
        animationList = new LinkedList<AnimationFrame>();
        File newFile = new File(filename);
        currentgroup = -1;
        regulate = true;
        sortAction = new Vector<LuaAction>();
        scanContents(newFile);
        buttonCleanup(0);
        control = new Thread(this);
        control.start();
        buttonAction(0);
    }    
    
    public void draw(Graphics2D g, JComponent screen){
        ListIterator<AnimationFrame> pointer = animationList.listIterator();
        boolean end = true;
        if(controlSpeed < 1){//if speed is invalid
            while(pointer.hasNext()){
                if(pointer.next().nextFrame(g,screen) != end)
                    end = false;
            }
        }else if(regulate){
            while(pointer.hasNext()){
                if(pointer.next().nextFrame(g,screen) != end)
                    end = false;
            }
            regulate = false;
        }else{
            while(pointer.hasNext()){
                if(pointer.next().currentFrame(g,screen) != end)
                    end = false;
            }
        }
        
        if(end)
            buttonAction(1001);
    }
    
    public void run() {
        try{
            while(true){
                control.sleep(controlSpeed);
                regulate = true;
            }
        }catch(Exception e){
            System.out.println(e.getStackTrace());
            System.exit(0);
        }
    }
    
    //handle key presses
    public void keyPressUp(){
        buttonAction(-1);
    }
    public void keyPressDown(){
        buttonAction(-2);
    }
    public void keyPressLeft(){
        buttonAction(-3);
    }
    public void keyPressRight(){
        buttonAction(-4);
    }
    public void keyPressB1(){
        buttonAction(1);
    }
    public void keyPressB2(){
        buttonAction(2);
    }
    public void keyPressB3(){
        buttonAction(3);
    }
    public void keyPressB4(){
        buttonAction(4);
    }
    public void keyPressB5(){
        buttonAction(5);
    }
    public void keyPressB6(){
        buttonAction(6);
    }
    public void keyPressB7(){
        buttonAction(7);
    }
    public void keyPressB8(){
        buttonAction(8);
    }
    public void keyPressB9(){
        buttonAction(9);
    }
    public void keyPressB10(){
        buttonAction(10);
    }
    
    private void buttonAction(int key){
        boolean clean = false;
        for(int i = 0; i < sortAction.size(); i++){
            if(sortAction.get(i).button == 1000){
                LuaEngine.runLuaString(sortAction.get(i).function);
                clean = true;
            }
            if(key == sortAction.get(i).button){
                if(currentgroup == sortAction.get(i).group){
                    LuaEngine.runLuaString(sortAction.get(i).function);
                    if(sortAction.get(i).groupchange < 0)
                        ScreenStack.pop();
                    else
                        currentgroup = sortAction.get(i).groupchange;
                    key = 0;
                }
            }  
        }
        if(clean)
            buttonCleanup(1000);
    }
    
    private Vector<LuaAction> scanContents(File theFile){
        Scanner sc = null;
        Vector<Integer> groupInts = new Vector<Integer>();
        String thePic = "";
        char theButton = '0';
        sortAction = new Vector<LuaAction>();
        
        try{
            sc = new Scanner(theFile);
        }catch(FileNotFoundException e){
            //error in file exchange
            System.err.println("Could not open "+theFile+"!");
        }
        
        if(sc.hasNextInt()){
            controlSpeed = sc.nextInt();
        }
        
        while(sc.hasNext()){
            if(sc.hasNextInt()){
                groupInts.add(sc.nextInt());
            }else{
                if(thePic.length() > 0){
                    if(thePic.charAt(0) == '-')
                        sortAction.add(
                                generalEdit(groupInts, theButton, thePic));
                    else
                        editAnimations(groupInts, thePic);  
                }
                //then clear all values
                thePic = sc.next();
                groupInts.clear();
                theButton = '0';
                //check the first value for a button
                if(sc.hasNextInt())
                    groupInts.add(sc.nextInt());
                else if(sc.hasNext()){
                    if(sc.hasNext("U") || sc.hasNext("u"))
                        theButton = 'U';
                    else if(sc.hasNext("D") || sc.hasNext("d"))
                        theButton = 'D';
                    else if(sc.hasNext("L") || sc.hasNext("l"))
                        theButton = 'L';
                    else if(sc.hasNext("R") || sc.hasNext("r"))
                        theButton = 'R';
                    else if(sc.hasNext("B") || sc.hasNext("b"))
                        theButton = 'B';
                    else if(sc.hasNext("E") || sc.hasNext("e"))
                        theButton = 'E';
                    sc.next();
                }
            }
            if(!sc.hasNext()){
                if(thePic.length() > 0){
                    if(thePic.charAt(0) == '-')
                        sortAction.add(
                                generalEdit(groupInts, theButton, thePic));
                    else
                        editAnimations(groupInts, thePic);
                }
            }
        }
        
        return sortAction;
    }
   
    
    private LuaAction generalEdit(Vector<Integer> theNumbers, 
            char key, String picture){
        //small error checking
        String function = "";
        String data = "";
        LuaAction create = new LuaAction();
        if(picture.length() > 4){
            function = picture.substring(1,4);
            data = picture.substring(5);
            
            System.out.println("In here:"+function);
            //my first attempt at Lua commands
            if(function.matches("pic")){   
                editAnimations(theNumbers, data);
            }
            else if(function.matches("lua")){
                create.function = data;
                if(key == '0'){
                    if(theNumbers.size() > 1){
                        create.button = theNumbers.get(0);
                        create.group = theNumbers.get(1);
                        if(theNumbers.size() > 2){
                            create.groupchange = theNumbers.get(2);
                        }
                    }
                }else{
                    create.bttn = key;
                    if(theNumbers.size() > 0){
                        create.group = theNumbers.get(0);
                        if(theNumbers.size() > 1){
                            create.groupchange = theNumbers.get(1);
                        }
                    }
                    create.fixButtons();
                }
            }
        }
        if(currentgroup < 0)
            currentgroup = create.group;
        return create;
    }
    
    private void editAnimations(Vector<Integer> theNumbers, String picture){
        if(theNumbers.size() > 0){
            //Make a new animation frame
            AnimationFrame tempFrame = new AnimationFrame();
            //If index matches a existing frame, add image to that frame
            if(animationList.size() != 0){
                if(theNumbers.get(0) >= 0 && 
                    theNumbers.get(0) < animationList.size()){
                    tempFrame = animationList.get(theNumbers.get(0));
                }
            }
               
            //Get the Image
            ImageHolder tempHolder;
            //store all values to LinkedList
            if(theNumbers.size() > 3){
                //The locations
                //store location for the first frame only
                if(tempFrame.store.size() == 0){
                    tempFrame.locx = (double)theNumbers.get(1);
                    tempFrame.locy = (double)theNumbers.get(2);
                }
                //Makes sure the image stands still
                tempHolder = new ImageHolder(
                        theNumbers.get(1), theNumbers.get(2));
                tempHolder.theString = picture;
                tempHolder.loop = theNumbers.get(3);
                if(theNumbers.size() > 7){
                    tempHolder.cutptx = theNumbers.get(4);
                    tempHolder.cutpty = theNumbers.get(5);
                    tempHolder.cutsx = theNumbers.get(6);
                    tempHolder.cutsy = theNumbers.get(7);
                }else if(theNumbers.size() > 5){
                    tempHolder.finalx = theNumbers.get(4);
                    tempHolder.finaly = theNumbers.get(5);
                }
                tempFrame.store.add(tempHolder);
            }
            //store to the linkedList
            if(theNumbers.get(0) > -1 && 
                    theNumbers.get(0) < animationList.size())
                animationList.set(theNumbers.get(0),tempFrame);
            else
                animationList.add(tempFrame);
        }
    }  
    
    private void buttonCleanup(int bttn){
        for(int i = 0; i < sortAction.size(); i++){
            if(sortAction.get(i).button == bttn){
                sortAction.remove(i);
                if(i > 0)
                    i--;
            }else if(i == 0)
                currentgroup = sortAction.get(i).group;
        }
    }
}


